package com.wigem.approve.service;

import java.util.List;

import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.wigem.approve.entity.DicEstateEntity;
import com.wigem.approve.mapper.DicEstateMapper;
import com.wigem.approve.vo.DicEstateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 楼宇字典Service业务层处理
 *
 * @author DH
 * @date 2024-05-27
 */
@Service
public class DicEstateServiceImpl implements DicEstateService {

    @Resource
    private DicEstateMapper dicEstateMapper;


    @Override
    public List<DicEstateEntity> getList(DicEstateVo dicEstatevo) {

        return dicEstateMapper.selectList(query(dicEstatevo));
    }

    @Override
    public PageResult<DicEstateEntity> getPageList(DicEstateVo dicEstateVo) {

        return dicEstateMapper.selectPage(dicEstateVo, query(dicEstateVo));
    }


    @Override
    public DicEstateEntity getEntity(String id) {
        return dicEstateMapper.selectById(id);
    }

    @Override
    public void add(DicEstateVo dicEstateVo) {
        DicEstateEntity dicEstateEntity = new DicEstateEntity();
        BeanUtils.copyProperties(dicEstateVo, dicEstateEntity);
        dicEstateEntity.create();
        dicEstateMapper.insert(dicEstateEntity);
    }


    @Override
    public void update(DicEstateVo dicEstateVo) {
        DicEstateEntity dicEstateEntity = new DicEstateEntity();
        BeanUtils.copyProperties(dicEstateVo, dicEstateEntity);
        dicEstateMapper.updateById(dicEstateEntity);
    }

    @Override
    public void save(DicEstateVo dicEstateVo) {

        DicEstateEntity dicEstateEntity = new DicEstateEntity();
        BeanUtils.copyProperties(dicEstateVo, dicEstateEntity);
        if (StringUtils.isEmpty(dicEstateVo.getId())) {
            dicEstateEntity.create();
            dicEstateMapper.insert(dicEstateEntity);
        } else {
            dicEstateMapper.updateById(dicEstateEntity);
        }

    }


    @Override
    public int delete(String id) {
        return dicEstateMapper.deleteById(id);
    }


    @Override
    public int remove(String id) {
        return dicEstateMapper.remove(id);
    }


    public LambdaQueryWrapperX<DicEstateEntity> query(DicEstateVo dicEstateVo) {

        LambdaQueryWrapperX<DicEstateEntity> dicEstateEntityLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        dicEstateEntityLambdaQueryWrapperX = dicEstateEntityLambdaQueryWrapperX
                .eqIfPresent(DicEstateEntity::getId, dicEstateVo.getId())
                .likeIfPresent(DicEstateEntity::getEstateName, dicEstateVo.getEstateName())
                .eqIfPresent(DicEstateEntity::getRegionId, dicEstateVo.getRegionId())
                .eqIfPresent(DicEstateEntity::getRemoveMark, dicEstateVo.getRemoveMark())

        ;
        return dicEstateEntityLambdaQueryWrapperX;
    }


}
